import Vue from 'vue'
import VueRouter from 'vue-router'
// 导包
import login from '../views/user/login.vue'
import home from '../views/home/home.vue'
import movie from '../views/movie/movie.vue'
import QA from '../views/QA/QA.vue'
import user from '../views/user/user.vue'
import layout from '../views/layout.vue'
import notfound from '../views/notfound/notfound.vue'
import edit from '../views/user/edit.vue'
import {
    getToken
} from '../utils/token'
import {
    Toast
} from 'vant';
import details from '../views/home/details.vue'
import search from '../views/home/search.vue'



Vue.use(VueRouter)

const routes = [{
        name: login,
        path: '/login',
        component: login
    },
    {
        name: edit,
        path: '/edit',
        component: edit
    },
    {
        name: search,
        path: '/search',
        component: search
    },
    {
        name: details,
        path: '/details',
        component: details
    },
    {
        name: layout,
        path: '/',
        component: layout,
        children: [{
            name: home,
            path: '/home',
            component: home
        }, {
            name: movie,
            path: '/movie',
            component: movie
        }, {
            name: QA,
            path: '/QA',
            component: QA
        }, {
            name: user,
            path: '/user',
            component: user,
            meta: {
                isLogin: true
            }
        }, {

            path: '/',
            redirect: '/home'
        }, ]
    },
    {
        name: notfound,
        path: '/notfound',
        component: notfound
    }, {
        path: '*',
        redirect: '/notfound'
    }
]

const router = new VueRouter({
        routes
    })
    // 注册全局前置守卫
router.beforeEach((to, from, next) => {
    if (to.meta.isLogin && !getToken()) {
        Toast.fail('请先登录');
        next({
            path: '/login'
        })
    } else {
        next()
    }
})

export default router